package com.fc.test.controller.admin;

import com.fc.test.mapper.auto.ForeClassMapper;
import com.fc.test.mapper.auto.ForeUserMapper;
import com.fc.test.model.auto.ForeClass;
import com.fc.test.model.auto.ForeUser;
import com.fc.test.model.auto.TsysUser;
import com.fc.test.vo.ForeClassUserVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.PageInfo;
import com.fc.test.common.base.BaseController;
import com.fc.test.common.domain.AjaxResult;
import com.fc.test.model.auto.ForeClassUser;
import com.fc.test.model.custom.TableSplitResult;
import com.fc.test.model.custom.Tablepar;
import com.fc.test.model.custom.TitleVo;
import com.fc.test.service.ForeClassUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("ForeClassUserController")
@Api(value = "")
public class ForeClassUserController extends BaseController{

	private String prefix = "gen/foreClassUser";
	@Autowired
	private ForeClassUserService foreClassUserService;

	@GetMapping("view")
    public String view(ModelMap model)
    {
		String str="";
		setTitle(model, new TitleVo("列表", str+"管理", true,"欢迎进入"+str+"页面", true, false));
        return prefix + "/list";
    }

	//@Log(title = "集合查询", action = "111")
	@PostMapping("list")
	@ResponseBody
	public Object list(Tablepar tablepar,String searchText){

		TsysUser foreUser= (TsysUser) SecurityUtils.getSubject().getPrincipal();
		PageInfo<ForeClassUser> page=foreClassUserService.list(tablepar,searchText,foreUser.getId()) ;

		List<ForeClassUserVo> list=new ArrayList<>();
		for(ForeClassUser foreClassUser:page.getList()){
			ForeClassUserVo vo=new ForeClassUserVo();
			BeanUtils.copyProperties(foreClassUser,vo);
	        vo.setUsername(getUsername(foreClassUser));
	        vo.setClassName(getClassname(foreClassUser));
	        list.add(vo);
		}

		TableSplitResult<ForeClassUserVo> result=new TableSplitResult<ForeClassUserVo>(page.getPageNum(), page.getTotal(), list);

		return  result;
	}

	@Autowired
	ForeClassMapper foreClassMapperl;
	@Autowired
	ForeUserMapper foreUserMapper;
	private String getClassname(ForeClassUser foreClassUser) {
		ForeClass foreClass =foreClassMapperl.selectByPrimaryKey(foreClassUser.getClassId());
		return foreClass!=null?foreClass.getName():"";
	}

	private String getUsername(ForeClassUser foreClassUser) {
		ForeUser foreUser =foreUserMapper.selectByPrimaryKey(foreClassUser.getUserId());
		return foreUser!=null?foreUser.getUsername():"";
	}


	/**
     * 新增
     */

    @GetMapping("/add")
    public String add(ModelMap modelMap)
    {
        return prefix + "/add";
    }

	//@Log(title = "新增", action = "111")
	@PostMapping("add")
	@ResponseBody
	public AjaxResult add(ForeClassUser foreClassUser){
		int b=foreClassUserService.insertSelective(foreClassUser);
		if(b>0){
			return success();
		}else{
			return error();
		}
	}

	/**
	 * 删除用户
	 * @param ids
	 * @return
	 */
	//@Log(title = "删除", action = "111")
	@PostMapping("remove")
	@ResponseBody
	public AjaxResult remove(String ids){
		int b=foreClassUserService.deleteByPrimaryKey(ids);
		if(b>0){
			return success();
		}else{
			return error();
		}
	}

	/**
	 * 检查用户
	 * @param tsysUser
	 * @return
	 */
	@PostMapping("checkNameUnique")
	@ResponseBody
	public int checkNameUnique(ForeClassUser foreClassUser){
		int b=foreClassUserService.checkNameUnique(foreClassUser);
		if(b>0){
			return 1;
		}else{
			return 0;
		}
	}


	/**
	 * 修改跳转
	 * @param id
	 * @param mmap
	 * @return
	 */
	@GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        mmap.put("ForeClassUser", foreClassUserService.selectByPrimaryKey(id));

        return prefix + "/edit";
    }

	/**
     * 修改保存
     */
    //@Log(title = "修改", action = "111")
    @RequiresPermissions("gen:foreClassUser:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ForeClassUser record)
    {
        return toAjax(foreClassUserService.updateByPrimaryKeySelective(record));
    }


    /**
   	 * 根据主键查询
   	 *
   	 * @param id
   	 * @param mmap
   	 * @return
   	 */
   	@PostMapping("/get/{id}")
   	@ApiOperation(value = "根据id查询唯一", notes = "根据id查询唯一")
   	public ForeClassUser edit(@PathVariable("id") String id) {
   		return foreClassUserService.selectByPrimaryKey(id);
   	}



}
